package mysql

import (
	"gorm.io/gorm"
	"novel-world/novelrpc/novelrpc"
)

type User struct {
	gorm.Model

	Mobile   string `gorm:"type:varchar(20);not null"`
	Password string `gorm:"type:char(32);not null"`
	Image    string `gorm:"type:varchar(500);not null"`
	Title    string `gorm:"type:varchar(20);not null"`
}

func (u *User) LoginAdd(db *gorm.DB, mobile string) error {
	return db.Debug().Where("mobile = ?", mobile).FirstOrCreate(u).Error
}

func (u *User) PasswordFirst(db *gorm.DB, mobile string) error {
	return db.Debug().Where("mobile = ?", mobile).First(u).Error
}

func (u *User) UpdatePassword(db *gorm.DB, mobile string) error {
	return db.Debug().Where("mobile = ?", mobile).Updates(u).Error
}

type Novel struct {
	gorm.Model
	Name    string `gorm:"type:varchar(20);not null"`
	Type    string `gorm:"type:varchar(20);not null"`
	Label   string `gorm:"type:varchar(20);not null"`
	Content string `gorm:"type:varchar(100);not null"`
	UserId  int    `gorm:"type:int(11);not null"`
}

func (n *Novel) NovelFind(db *gorm.DB, name string) error {
	return db.Debug().Where("name = ?", name).First(n).Error
}

func (n *Novel) NovelAdd(db *gorm.DB) error {
	return db.Debug().Create(&n).Error
}

func (n *Novel) ListNovel(db *gorm.DB, list []*novelrpc.GetList, in *novelrpc.NovelListRequest) []*novelrpc.GetList {
	err1 := db.Debug().Model(&n).Table("novels").
		Select(
			"users.image as Image",
			"users.title as Title",
			"novels.`name` as Name",
			"novels.type as Type",
			"novels.label as Label",
			"novels.content as Content",
		).
		Joins("LEFT JOIN users ON users.id = novels.user_id").
		Order("novels.created_at desc").
		Scopes(Paginate(int(in.Page), int(in.Size)))
	if in.Name != "" {
		err1 = err1.Where("name = ?", in.Name)
	}
	if in.Type != "" {
		err1 = err1.Where("type = ?", in.Type)
	}
	err1.Find(&list)
	return list
}

type Goods struct {
	gorm.Model
	UserId  int    `gorm:"type:int(11);not null"`
	NovelId int    `gorm:"type:int(11);not null"`
	Comment string `gorm:"type:varchar(100);not null"`
}

func (g *Goods) GoodsAdd(db *gorm.DB) error {
	return db.Debug().Create(&g).Error
}

func (g *Goods) GoodsList(db *gorm.DB, lists []*novelrpc.GetList2, in *novelrpc.GoodsListRequest) ([]*novelrpc.GetList2, error) {
	tx := db.Debug().Model(&g).Table("goods").
		Select(
			"users.image as Image",
			"users.title as Title",
			"novels.`name` as Name",
			"goods.comment as comment",
		).
		Joins("LEFT JOIN users ON users.id = goods.user_id").
		Joins("LEFT JOIN novels ON novels.id = goods.novel_id").
		Where("novels.name = ?", in.Name).
		Order("goods.created_at desc").
		Find(&lists).Error
	return lists, tx
}

type Books struct {
	gorm.Model
	UserId  int `gorm:"type:int(11);not null"`
	NovelId int `gorm:"type:int(11);not null"`
}

func (b *Books) BooksList(db *gorm.DB, lists []*novelrpc.GetList1, in *novelrpc.BooksListRequest) ([]*novelrpc.GetList1, error) {
	err2 := db.Debug().Model(&b).Table("books").
		Select(
			"users.image as Image",
			"users.title as Title",
			"novels.`name` as Name",
			"novels.type as Type",
		).
		Joins("LEFT JOIN users ON users.id = books.user_id").
		Joins("LEFT JOIN novels on novels.id = books.novel_id").
		Where("title = ?", in.Title).Find(&lists).Error
	return lists, err2
}

func (b *Books) BooksAdd(db *gorm.DB) error {
	return db.Debug().Create(&b).Error
}

func (b *Books) BooksFind(db *gorm.DB, booksId int64) error {
	return db.Debug().Where("books.id = ?", booksId).First(b).Error
}

func (b *Books) BooksDel(db *gorm.DB, booksId int64) error {
	return db.Where("books.id = ?", booksId).Delete(b).Error
}
